package com.mybatis_multitable.mapper;

import com.mybatis_multitable.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

// implementation = PerpetualCache.class  指定实现二级缓存的class
@CacheNamespace(implementation = RedisCache.class)  //开启二级缓存  
public interface UserMapper {
    
    // 查询所有用户,同时查询每个用户关联的订单信息
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orders",column = "id",
                    javaType = List.class,many = @Many(select = "com.mybatis_multitable.mapper.OrderMapper.findByUid"))
    })
    @Select("select * from user")
    List<User> findUserAndOrder();
    
    //查询所有用户,同时查询每个用户关联的角色信息
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "roles",column = "id", javaType = List.class,
                    many = @Many(select = "com.mybatis_multitable.mapper.RoleMapper.findRoleByUid"))
    })
    List<User> findUserAndRole();
    
    //添加用户
    @Insert("insert into user values (#{id},#{username},#{password},#{birthday})")
    void addUser(User user);
    
    //更新用户
    @Update("update user set username = #{username} where id = #{id}")
    void updateUser(User user);
    
    //查询用户
    @Select("select * from user")
    List<User> getUser();
    
    //删除用户
    @Delete("delete from user where id = #{id}")
    void deleteUser(Integer id);

    //是否使用二级缓存  默认为true
//    @Options(useCache = false)
    @Select("select * from user where id = #{id}")
    User getUserByUid(Integer id);
}
